/**
 * 
 */
package com.ai.notices.utils.mongo;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;

import net.sf.json.JSONObject;

/**
 * @author microzhu
 *
 */
@Service
public class MongoSVImpl implements ConfigurationWatcher, MongoSV {
	private static Logger log = LoggerFactory.getLogger(MongoSVImpl.class);
	private String confPath = "/com/ai/paas/mongodb/conf";
    private static final String USERNAME_KEY = "username";
    private static final String PASSWORD_KEY = "password";
    private static final String SERVER_KEY="fileServer";
    private static final String DBNAME_KEY="fileRepo";
    private String configurationFile="/PaasConfigurationFile.properties";
    private String username=null;
    private String password=null;
    private String server=null;
    private String dbname=null;
    private MongoDBClient mongo = null;
    private  Properties props = new Properties();
    
    @PostConstruct  
    public void init() {
        try {
        	this.props.load(this.getClass().getResourceAsStream(configurationFile));
        	String path = (String)this.props.get(confPath);
        	process(path);
        } catch (Exception e) {
            log.error("mongodb连接初始化错误。",e);
        }
    }
	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#insert(java.lang.String, java.lang.String)
	 */
	@Override
	public String insert(String collectionName, String doc) {
		// TODO Auto-generated method stub
		return mongo.insert(dbname,collectionName,doc);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#insert(java.lang.String, net.sf.json.JSONObject)
	 */
	@Override
	public String insert(String collectionName, JSONObject doc) {
		// TODO Auto-generated method stub
		 return mongo.insert(dbname,collectionName,doc);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#insert(java.lang.String, java.util.Map)
	 */
	@Override
	public String insert(String collectionName, Map docMap) {
		// TODO Auto-generated method stub
		return mongo.insert(dbname,collectionName,docMap);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#insert(java.lang.String, java.lang.Object)
	 */
	@Override
	public String insert(String collectionName, Object obj) {
		// TODO Auto-generated method stub
		return mongo.insert(dbname,collectionName,obj);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#insertList(java.lang.String, net.sf.json.JSONObject[])
	 */
	@Override
	public List<String> insertList(String collectionName, JSONObject... docs) {
		// TODO Auto-generated method stub
		return mongo.insertList(dbname,collectionName,docs);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#insertList(java.lang.String, java.util.List)
	 */
	@Override
	public List<String> insertList(String collectionName, List<Map> docMapList) {
		// TODO Auto-generated method stub
		return mongo.insertList(dbname,collectionName,docMapList);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#getDBCollection(java.lang.String)
	 */
	@Override
	public DBCollection getDBCollection(String collectionName) {
		// TODO Auto-generated method stub
		return mongo.getDBCollection(dbname,collectionName);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#findOne(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject, com.mongodb.DBObject)
	 */
	@Override
	public DBObject findOne(String collectionName, DBObject condition, DBObject field, DBObject orderBy) {
		// TODO Auto-generated method stub
		 return mongo.findOne(dbname,collectionName,condition,field,orderBy);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#update(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject)
	 */
	@Override
	public WriteResult update(String collectionName, DBObject query, DBObject o) {
		// TODO Auto-generated method stub
		 return mongo.update(dbname,collectionName,query,o);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.MongoSV#remove(java.lang.String, com.mongodb.DBObject)
	 */
	@Override
	public WriteResult remove(String collectionName, DBObject query) {
		// TODO Auto-generated method stub
		return mongo.remove(dbname,collectionName,query);
	}

	/* (non-Javadoc)
	 * @see com.ai.notices.utils.mongo.ConfigurationWatcher#process(java.lang.String)
	 */
	@Override
	public void process(String conf) {
		// TODO Auto-generated method stub

        if(log.isInfoEnabled()) {
            log.info("new log configuration is received: " + conf);
        }
        JSONObject json = JSONObject.fromObject(conf);
        boolean changed = false;

        if(json.containsKey(SERVER_KEY) && !json.getString(SERVER_KEY).equals(server)) {
            changed = true;
            server = json.getString(SERVER_KEY);
        }
        if(json.containsKey(DBNAME_KEY) && !json.getString(DBNAME_KEY).equals(dbname)) {
            changed = true;
            dbname = json.getString(DBNAME_KEY);
        }
        if(json.containsKey(USERNAME_KEY) && !json.getString(USERNAME_KEY).equals(username)) {
            changed = true;
            username = json.getString(USERNAME_KEY);
        }
        if(json.containsKey(PASSWORD_KEY) && !json.getString(PASSWORD_KEY).equals(password)) {
            changed = true;
            password = json.getString(PASSWORD_KEY);
        }

        if(changed) {
            if(server != null) {
                mongo = new MongoDBClient(server,dbname, username, password);
                if(log.isInfoEnabled()) {
                    log.info("log server address is changed to " + server);
                }
            }
        }
    
	}
	public static void main(String[] args) throws Exception{
		MongoSVImpl impl = new MongoSVImpl();
		impl.props.load(impl.getClass().getResourceAsStream("/PaasConfigurationFile.properties"));
    	String s = (String)impl.props.get("/com/ai/paas/mongodb/conf");
    	System.out.println(s);
	}
}
